package com.slashvision.im

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.slashvision.im.R
import io.openim.android.sdk.OpenIMClient
import io.openim.android.sdk.listener.*
import io.openim.android.sdk.models.*
import io.openim.android.sdk.utils.JsonUtil

open class InitIMActivity : AppCompatActivity(), InitSDKListener, OnAdvanceMsgListener, OnConversationListener, OnFriendshipListener, OnGroupListener {

    /// api地址
    val IP_API = "http://1.14.194.38:10000"
    /// websocket地址
    val IP_WS = "ws://1.14.194.38:17778"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val path: String = getExternalCacheDir()?.getAbsolutePath()?:""
        val client = OpenIMClient.getInstance()
        client.initSDK(IP_API, IP_WS, path, this)
        client.messageManager.addAdvancedMsgListener(this)
        client.groupManager.setOnGroupListener(this)
        client.conversationManager.setOnConversationListener(this)
        client.friendshipManager.setOnFriendshipListener(this)
        // uid: 13918588195, token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

        Handler(mainLooper).postDelayed({onLogin()}, 1000)
    }

    override fun onConnectFailed(code: Long, error: String?) {
        println("=======onConnectFailed=================")
    }

    override fun onConnectSuccess() {
        println("===========onConnectSuccess=============")
    }

    override fun onConnecting() {
        println("==========onConnecting==============")
    }

    override fun onKickedOffline() {
        println("==========onKickedOffline==============")
    }

    override fun onSelfInfoUpdated(info: UserInfo?) {
        println("==========onSelfInfoUpdated==============")
    }

    override fun onUserTokenExpired() {
        println("=========onUserTokenExpired===============")
    }

    override fun onRecvNewMessage(msg: Message?) {
        println("=========onRecvNewMessage===============")
    }

    override fun onRecvC2CReadReceipt(list: List<HaveReadInfo?>?) {
        println("========onRecvC2CReadReceipt================")
    }

    override fun onRecvMessageRevoked(msgId: String?) {
        println("=======onRecvMessageRevoked=================")
    }

    override fun onConversationChanged(list: List<ConversationInfo?>?) {
        println("========onConversationChanged================")
    }

    override fun onNewConversation(list: List<ConversationInfo?>?) {
        println("==========onNewConversation==============")
    }

    override fun onSyncServerFailed() {
        println("========onSyncServerFailed================")
    }

    override fun onSyncServerFinish() {
        println("=========onSyncServerFinish===============")
    }

    override fun onSyncServerStart() {
        println("=========onSyncServerStart===============")
    }

    override fun onTotalUnreadMessageCountChanged(i: Int) {
        println("==========onTotalUnreadMessageCountChanged==============")
    }

    override fun onBlackListAdd(u: UserInfo?) {
        println("==========onBlackListAdd==============")
    }

    override fun onBlackListDeleted(u: UserInfo?) {
        println("=======onBlackListDeleted=================")
    }

    override fun onFriendApplicationListAccept(u: UserInfo?) {
        println("=========onFriendApplicationListAccept===============")
    }

    override fun onFriendApplicationListAdded(u: UserInfo?) {
        println("==========onFriendApplicationListAdded==============")
    }

    override fun onFriendApplicationListDeleted(u: UserInfo?) {
        println("======onFriendApplicationListDeleted==================")
    }

    override fun onFriendApplicationListReject(u: UserInfo?) {
        println("=======onFriendApplicationListReject=================")
    }

    override fun onFriendInfoChanged(u: UserInfo?) {
        println("=====onFriendInfoChanged===================")
    }

    override fun onFriendListAdded(u: UserInfo?) {
        println("========onFriendListAdded================")
    }

    override fun onFriendListDeleted(u: UserInfo?) {
        println("========onFriendListDeleted================")
    }

    override fun onMemberEnter(groupId: String?, list: List<GroupMembersInfo?>?) {
        println("========onMemberEnter================")
    }

    override fun onMemberLeave(groupId: String?, info: GroupMembersInfo?) {
        println("=========onMemberLeave===============")
    }

    override fun onMemberInvited(
        groupId: String?,
        opUser: GroupMembersInfo?,
        list: List<GroupMembersInfo?>?
    ) {
        println("=========onMemberInvited===============")
    }

    override fun onMemberKicked(
        groupId: String?,
        opUser: GroupMembersInfo?,
        list: List<GroupMembersInfo?>?
    ) {
        println("=========onMemberKicked===============")
    }

    override fun onGroupCreated(groupId: String?) {
        println("==========onGroupCreated==============")
    }

    override fun onGroupInfoChanged(groupId: String?, info: GroupInfo?) {
        println("===========onGroupInfoChanged=============")
    }

    override fun onReceiveJoinApplication(groupId: String?, info: GroupMembersInfo?, opReason: String?) {
        println("===========onReceiveJoinApplication=============")
    }

    override fun onApplicationProcessed(
        groupId: String?,
        opUser: GroupMembersInfo?,
        agreeOrReject: Int,
        opReason: String?
    ) {
        println("============onApplicationProcessed============")
    }

    fun onGetConversation(view: View?) {
        OpenIMClient.getInstance().conversationManager.getAllConversationList(object :
            OnBase<List<ConversationInfo?>?> {
            override fun onError(code: Long, error: String) {}
            override fun onSuccess(data: List<ConversationInfo?>?) {
                println("onGetConversation:" + JsonUtil.toString(data))
            }
        })
    }

    fun onLogin() {
        OpenIMClient.getInstance().login(
            object : OnBase<String?> {
                override fun onError(code: Long, error: String) {}
                override fun onSuccess(data: String?) {
                    println("登录成功!")
                }
            },
            "13918588195",
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiIxMzkxODU4ODE5NSIsIlBsYXRmb3JtIjoiQW5kcm9pZCIsImV4cCI6MTYzNDM3ODE3MSwiaWF0IjoxNjMzNzczMzcxLCJuYmYiOjE2MzM3NzMzNzF9.d6vAIBlbOF2r7Ouz2Opl6GvroDpHkGCp-QNxm5cKiFU"
        )
    }

    fun onGetMessageHistory(view: View?) {
        OpenIMClient.getInstance().messageManager.getHistoryMessageList(object :
            OnBase<List<Message?>> {
            override fun onError(code: Long, error: String) {}
            override fun onSuccess(data: List<Message?>) {
                println("消息数：" + data.size)
            }
        }, "0b55c23c7fac2b34", "", null, 20)
    }
}
